package com.saoft.fangcms.common.config.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 项目配置项
 * @author 小懒虫
 * @date 2018/11/6
 */
@Component
@ConfigurationProperties(prefix = "project")
public class ProjectProperties {

    /** 是否开启验证码 */
    private boolean captchaOpen = false;

    /** xss防护设置 */
    private ProjectProperties.Xxs xxs = new ProjectProperties.Xxs();

    public boolean isCaptchaOpen() {
        return captchaOpen;
    }

    public void setCaptchaOpen(boolean captchaOpen) {
        this.captchaOpen = captchaOpen;
    }

    public Xxs getXxs() {
        return xxs;
    }

    public void setXxs(Xxs xxs) {
        this.xxs = xxs;
    }

    /**
     * xss防护设置
     */
    public static class Xxs {
        /** xss防护开关 */
        private boolean enabled = true;

        /** 拦截规则，可通过“,”隔开多个 */
        private String urlPatterns = "/*";

        /** 默认忽略规则（无需修改） */
        private String defaultExcludes = "/favicon.ico,/img/*,/js/*,/css/*,/lib/*";

        /** 忽略规则，可通过“,”隔开多个 */
        private String excludes = "";

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public String getUrlPatterns() {
            return urlPatterns;
        }

        public void setUrlPatterns(String urlPatterns) {
            this.urlPatterns = urlPatterns;
        }

        public String getDefaultExcludes() {
            return defaultExcludes;
        }

        public void setDefaultExcludes(String defaultExcludes) {
            this.defaultExcludes = defaultExcludes;
        }

        public void setExcludes(String excludes) {
            this.excludes = excludes;
        }

        /**
         * 拼接忽略规则
         */
        public String getExcludes() {
            if (!StringUtils.isEmpty(excludes.trim())) {
                return defaultExcludes + "," + excludes;
            }
            return defaultExcludes;
        }
    }
}
